package com.feicheng.controller.main.admin.store;


import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Access;
import com.feicheng.service.impl.FCheckCodeServiceImpl;
import com.feicheng.service.impl.FStoreServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/main/admin/store/access")
@Slf4j
public class AccessController {
    @Autowired
    FStoreServiceImpl fStoreService;

    @Autowired
    FCheckCodeServiceImpl ifCheckCodeService;
    //总店分页查询门禁设备列表
    @GetMapping("/page")
    public ResultInfo getAccessByPage(String param) {
        //判断参数是否为空
        if(null == param) {
            return fStoreService.getAccessesByPage(null);
        } else {
            //校验参数是否为json字符串
            try {
                return fStoreService.getAccessesByPage(JSONObject.parseObject(param));
            }catch (JSONException jsonException) {
                //TODO 传入的参数不符合json格式
                throw new SystemException("");
            }
        }
    }
    //根据id获得门禁详情
    @GetMapping
    public ResultInfo getAccess(Integer id){
        return fStoreService.getAccess(id);
    }
    //根据门禁id修改门禁详情和门禁-门店的关系
    @PutMapping
    public ResultInfo changeAccess(String access,Integer storeId, Boolean ioFlg){
        //校验参数是否为json字符串
        try {
            //String转实体类
            Access updateAccess=JSONObject.parseObject(access,Access.class);
            return fStoreService.updateAccess(updateAccess,storeId,ioFlg);
        }catch (JSONException jsonException){
            //TODO 传入的参数不符合json格式
            throw new SystemException("");
        }
    }
        //根据门禁id删除门禁
    @DeleteMapping("/info")
    public ResultInfo delAccess(Integer accessId){
        return  fStoreService.deleteAccess(accessId);
    }
    //新增门禁
    @PostMapping("/info")
    public ResultInfo newAccess(String access, Integer storeId, Boolean ioFlg){
        //校验参数是否为json字符串
        try {
            //String转实体类
            Access newAccess=JSONObject.parseObject(access,Access.class);
            return fStoreService.newAccess(newAccess,storeId,ioFlg);
        }catch (JSONException jsonException){
            //TODO 传入的参数不符合json格式
            throw new SystemException("");
        }
    }
    /**
     * 接收远程命令
     */
    @PostMapping("/cmd")
    public ResultInfo newCmd(Integer accessId, Integer CmdCode) {
        return ifCheckCodeService.newCmdToMap(accessId, CmdCode);
    }
}
